package com.smartcat.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.smartcat.bean.LoginUserBean;
import com.smartcat.model.FunctionConfig;
import com.smartcat.service.LoadDataService;


public class JspHelper implements ServletContextAware{

	static JspHelper helper;
	private  ServletContext servletContext;
	
	private static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
	public JspHelper(){
		helper=this;
	}
	
	public static synchronized JspHelper getInstantce(){
	   return helper;
	}
	
	
	private Object getService(String beanName){
		return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getBean(beanName);
	}
	
	public List<FunctionConfig> loadFunctions(){
		LoadDataService loadData=(LoadDataService)getService("loadDataService");
		
		return loadData.loadFunctions();
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext=arg0;
	}
	
	public static String formatDate(Date date){
		return dateFormat.format(date);
	}
	
	
	public Map<Integer,FunctionConfig> loadUserFunction(LoginUserBean user){
		LoadDataService loadData=(LoadDataService)getService("loadDataService");
		return loadData.loadUserFunction(user);
	}
	
	public static Object getValue(String name){
		Object obj=null;
		obj=ActionContext.getContext().getValueStack().findValue(name);
		return obj;
	}
}
